<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;
use Think\Page;

/**
 * 后台报名信息管理控制器
 * @author huajie <banhuajie@163.com>
 */
class VolunteerController extends AdminController {



    /**
     * 显示左边菜单，进行权限控制
     * @author huajie <banhuajie@163.com>
     */
    protected function getMenu(){
        //获取动态分类
        $cate_auth  =   AuthGroupModel::getAuthCategories(UID);	//获取当前用户所有的内容权限节点
        $cate_auth  =   $cate_auth == null ? array() : $cate_auth;
        $cate       =   M('Category')->where(array('status'=>1))->field('id,title,pid,allow_publish')->order('pid,sort')->select();

        //没有权限的分类则不显示
        if(!IS_ROOT){
            foreach ($cate as $key=>$value){
                if(!in_array($value['id'], $cate_auth)){
                    unset($cate[$key]);
                }
            }
        }

        $cate           =   list_to_tree($cate);	//生成分类树

        //获取分类id
        $cate_id        =   I('param.cate_id');
        $this->cate_id  =   $cate_id;

        //是否展开分类
        $hide_cate = false;
        if(ACTION_NAME != 'recycle' && ACTION_NAME != 'draftbox' && ACTION_NAME != 'mydocument'){
            $hide_cate  =   true;
        }

        //生成每个分类的url
        foreach ($cate as $key=>&$value){
            $value['url']   =   'Article/index?cate_id='.$value['id'];
            if($cate_id == $value['id'] && $hide_cate){
                $value['current'] = true;
            }else{
                $value['current'] = false;
            }
            if(!empty($value['_child'])){
                $is_child = false;
                foreach ($value['_child'] as $ka=>&$va){
                    $va['url']      =   'Article/index?cate_id='.$va['id'];
                    if(!empty($va['_child'])){
                        foreach ($va['_child'] as $k=>&$v){
                            $v['url']   =   'Article/index?cate_id='.$v['id'];
                            $v['pid']   =   $va['id'];
                            $is_child = $v['id'] == $cate_id ? true : false;
                        }
                    }
                    //展开子分类的父分类
                    if($va['id'] == $cate_id || $is_child){
                        $is_child = false;
                        if($hide_cate){
                            $value['current']   =   true;
                            $va['current']      =   true;
                        }else{
                            $value['current'] 	= 	false;
                            $va['current']      =   false;
                        }
                    }else{
                        $va['current']      =   false;
                    }
                }
            }
        }

        $this->assign('nodes',      $cate);
        $this->assign('cate_id',    $this->cate_id);

        //获取面包屑信息
        $nav = get_parent_category($cate_id);
        $this->assign('rightNav',   $nav);

        //获取回收站权限
        $show_recycle = $this->checkRule('Admin/article/recycle');
        $this->assign('show_recycle', IS_ROOT || $show_recycle);
        //获取草稿箱权限
        $this->assign('show_draftbox', C('OPEN_DRAFTBOX'));
    }


    /**非自主报名信息
     */
    public function involuntary(){
        $Model   =   M('student_info');
        $map['type']=2;
        $status=I('get.status');
        if($status=='' || $status==null){
            $map['status']=array('gt',-1);
        }else{
            $map['status']=$status;
        }

        $id=I('get.id');
        if($id!='' && $id!=null){
            $map['introducer_id']=$id;
        }
        $major=I('get.major');
        if($major!='' && $major!=null){
            $where['title']=array('like','%'.$major.'%');
            $major=M('major')->where($where)->select();
            $ids = array_column($major, 'id');
            $map['major']=array('in',$ids);
        }
        $name=I('get.name');
        if($name!='' && $name!=null){
            $map['name']=array('like','%'.$name.'%');
        }

        $ID_number=I('get.ID_number');
        if($ID_number!='' && $ID_number!=null){
            $map['ID_number']=array('like','%'.$ID_number.'%');
        }

        $introducer=I('get.introducer');
        if($introducer!='' && $introducer!=null){
            $map['introducer']=array('like','%'.$introducer.'%');
        }

        $list = $this->lists($Model,$map,'id desc');
        $this->assign('list', $list);
        $this->meta_title = '非自主报名信息';
        $this->display();
    }

    /**自主报名
     * @param null $search_word
     */
    public function voluntary($search_word=null){
        $Model   =   M('student_info');
        $map['type']=1;
        $status=I('get.status');
        if($status=='' || $status==null){
            $map['status']=array('gt',-1);
        }else{
            $map['status']=$status;
        }

        $id=I('get.id');
        if($id!='' && $id!=null){
            $map['id']=$id;
        }
        $major=I('get.major');
        if($major!='' && $major!=null){
            $where['title']=array('like','%'.$major.'%');
            $major=M('major')->where($where)->select();
            $ids = array_column($major, 'id');
            $map['major']=array('in',$ids);
        }
        $name=I('get.name');
        if($name!='' && $name!=null){
            $map['name']=array('like','%'.$name.'%');
        }

        $ID_number=I('get.ID_number');
        if($ID_number!='' && $ID_number!=null){
            $map['ID_number']=array('like','%'.$ID_number.'%');
        }
        $list = $this->lists($Model,$map,'update_time desc');
        $this->assign('list', $list);
        $this->meta_title = '非自主报名信息';
        $this->display();
    }

    //来访登记添加的信息
    public function specific(){
        $Model   =   M('student_info');
        $map['type']=array('in','3,5');
        $status=I('get.status');
        if($status=='' || $status==null){
            $map['status']=array('gt',-1);
        }else{
            $map['status']=$status;
        }

        $id=I('get.id');
        if($id!='' && $id!=null){
            $map['id']=$id;
        }

        $name=I('get.name');
        if($name!='' && $name!=null){
            $map['name']=array('like','%'.$name.'%');
        }

        $ID_number=I('get.ID_number');
        if($ID_number!='' && $ID_number!=null){
            $map['ID_number']=array('like','%'.$ID_number.'%');
        }
        $list = $this->lists($Model,$map,'create_time desc');
        $this->assign('list', $list);
        $this->meta_title = '来访登记信息';
        $this->display();
    }

    /**
     * 导出来访登记信息
     */
    public function exportSpecific(){
        $map['a.type']=array('in','3,5');

        $name=I('get.name');
        if($name!='' && $name!=null){
            $map['a.name']=array('like','%'.$name.'%');
        }
        $ID_number=I('get.ID_number');
        if($ID_number!='' && $ID_number!=null){
            $map['a.ID_number']=array('like','%'.$ID_number.'%');
        }
        $list=M('student_info')
            ->alias('a')
            ->join('vo_major as b ON a.major=b.id')
            ->field('a.id,a.name,a.ID_number,a.sex,b.title,a.create_time,a.type')
            ->where($map)
            ->order('create_time desc')
            ->select();
        foreach ($list as $k=>&$vo){
            $vo['sex']=($vo['sex']==0)?'男':'女';
            $vo['create_time']=time_format($vo['create_time']);
            $vo['type']=($vo['type']==5)?'是':'否';
        }
        exportexcel($list,array('编号','姓名','身份证号','性别','专业','提交时间','招生代理是否添加'),'来访登记信息');
    }


    //入学报到添加的信息
    public function report(){
        $Model   =   M('student_info');
        $map['is_report']=array('neq',0);

        $id=I('get.id');
        if($id!='' && $id!=null){
            $map['id']=$id;
        }
        $name=I('get.name');
        if($name!='' && $name!=null){
            $map['name']=array('like','%'.$name.'%');
        }

        $ID_number=I('get.ID_number');
        if($ID_number!='' && $ID_number!=null){
            $map['ID_number']=array('like','%'.$ID_number.'%');
        }
        $list = $this->lists($Model,$map,'update_time desc');
        $this->assign('list', $list);
        $this->meta_title = '入学报到信息';
        $this->display();
    }

    /**
     * 导出入学报到信息
     */
    public function exportReport(){
        $map['a.is_report']=array('neq',0);

        $name=I('get.name');
        if($name!='' && $name!=null){
            $map['a.name']=array('like','%'.$name.'%');
        }
        $ID_number=I('get.ID_number');
        if($ID_number!='' && $ID_number!=null){
            $map['a.ID_number']=array('like','%'.$ID_number.'%');
        }
        $list=M('student_info')
            ->alias('a')
            ->join('vo_major as b ON a.major=b.id')
            ->field('a.id,a.name,a.ID_number,a.sex,b.title,a.class_id,a.update_time,a.manage_update_time,a.is_report')
            ->where($map)
            ->order('update_time desc')
            ->select();
        foreach ($list as $k=>&$vo){
            $class=M('class')->where(array('id'=>$vo['class_id']))->field('title')->find();
            $vo['class_id']=$class['title'];
            $vo['sex']=($vo['sex']==0)?'男':'女';
            $vo['update_time']=time_format($vo['update_time']);
            $vo['manage_update_time']=time_format($vo['manage_update_time']);
            switch ($vo['is_report']){
                case 1:
                case 2:
                  $vo['is_report']="未报到";
                  break;
                case 3:
                  $vo['is_report']="已报到";
                  break;
            }
        }
        exportexcel($list,array('编号','姓名','身份证号','性别','专业','班级','提交时间','专业调整时间','是否报到'),'入学报到信息');
    }


    //修改入学报到信息
    public function editReportInfo(){
        if(IS_POST){
            $data=I('post.');
            if(empty($data)){
                $this->error ("请填写完整数据");
            }
            $data['manage_update_time']=time();
            $status = M('student_info')->where()->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('report') );
            }
        }else{
            $id=I('get.id');
            $data=M('student_info')->where(array('id'=>$id))->find();
            $major=M('major')->field('id,title')->select();
            $class=M('class')->field('id,title')->select();
            $this->assign('class',$class);
            $this->assign('major',$major);
            $this->assign('data',$data);
            $this->meta_title = '编辑信息';
            $this->display('editreportinfo');
        }
    }



    //对普通学生信息修改
    public function editInfo(){
        $id=I('get.id');
        $type=I('type');
        $data=M('student_info')->where(array('id'=>$id))->find();
        $major=M('major')->field('id,title')->select();
        $this->assign('major',$major);
        $this->assign('data',$data);
        $this->assign('type',$type);
        $this->meta_title = '编辑信息';
        $this->display('editinfo');
    }

    public function saveInfo(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        $data['manage_update_time']=time();
        $status = M('student_info')->where()->save($data);
        if(false === $status){
            $this->error ("更新出错");
        }else{
            switch ($data['type']) {
                case 1:
                    $url=U('voluntary');
                    break;
                case 2:
                    $url=U('involuntary');
                    break;
                case 3:
                case 5:
                    $url=U('specific');
                    break;    
            }
            $this->success ('更新成功！', $url);
        }
    }



    /**
     * 专业管理
     */
    public function major(){
        $list   = $this->lists(M('major'), array(),'id asc');
        $this->assign('_list', $list);
        $this->meta_title = '专业信息';
        $this->display();
    }

    public function addMajor(){
        $this->meta_title = '新增专业';
        $this->display('editmajor');
    }

    public function editMajor(){
        $id=I('get.id');
        $data=M('major')->where(array('id'=>$id))->field('id,title')->find();
        $this->assign('data',$data);
        $this->meta_title = '编辑专业';
        $this->display('editmajor');
    }

    public function saveMajor(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }

        if(empty($data['id'])){ //新增数据
            $data['create_time']=time();
            $res = M('major')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('major') );
            }
        } else { //更新数据
            $data['update_time']=time();
            $status = M('major')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('major') );
            }
        }
    }

    public function deleteMajor(){
        $id = array_unique((array)I('id',0));
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] =   array('in',$id);
        $re = M('major')->where($map)->delete();
        if($re!==false){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }


    /**
     *导出excel
     */
   public function exportExcel(){
       $data=I('get.');
       if(!isset($data['type']) || empty($data['type'])){
           $data['type']='all';
       }
       $map['a.type']=2;
       $list=array();
       switch ($data['type']){
           case 'all':
               $status=I('get.status');
               if($status=='' || $status==null){
                   $map['a.status']=array('gt',-1);
               }else{
                   $map['a.status']=$status;
               }
               $id=I('get.id');
               if($id!='' && $id!=null){
                   $map['a.introducer_id']=$id;
               }
               $major=I('get.major');
               if($major!='' && $major!=null){
                   $where['title']=array('like','%'.$major.'%');
                   $major=M('major')->where($where)->select();
                   $ids = array_column($major, 'id');
                   $map['a.major']=array('in',$ids);
               }
               $name=I('get.name');
               if($name!='' && $name!=null){
                   $map['a.name']=array('like','%'.$name.'%');
               }

               $ID_number=I('get.ID_number');
               if($ID_number!='' && $ID_number!=null){
                   $map['a.ID_number']=array('like','%'.$ID_number.'%');
               }
               $introducer=I('get.introducer');
               if($introducer!='' && $introducer!=null){
                   $map['a.introducer']=array('like','%'.$introducer.'%');
               }

               $list=M('student_info')
                   ->alias('a')
                   ->join('vo_major as b ON a.major=b.id')
                   ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                   ->where($map)
                   ->select();
               break;
           case 'agent':
               $map['introducer_id']=$data['id'];
               $list=M('student_info')
                   ->alias('a')
                   ->join('vo_major as b ON a.major=b.id')
                   ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                   ->where($map)
                   ->select();
               break;
           case 'department':
               $depart=M('department_info')->where(array('id'=>$data['id']))->field('id,title')->find();
               $agent=M('agent')->where(array('department'=>$depart['id']))->field('uid,username')->select();
               if(empty($agent)){
                   $ids = array_column($agent, 'uid');
                   $map['introducer_id']=array('in',$ids);
                   $list=M('student_info')
                       ->alias('a')
                       ->join('vo_major as b ON a.major=b.id')
                       ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                       ->where($map)
                       ->select();
               }
               break;
           case 'major':
               $map['major']=$data['id'];
               $list=M('student_info')
                   ->alias('a')
                   ->join('vo_major as b ON a.major=b.id')
                   ->field('a.id,a.name,a.ID_number,a.home_address,a.school,b.title,a.introducer,a.introducer_phone,a.pay_evidence,a.create_time,a.update_time,a.manage_update_time,a.status')
                   ->where($map)
                   ->select();
               break;
       }

       if(empty($list)){
           $this->error("没有数据可以导出");
       }
       foreach ($list as $k=>&$vo){
           $vo['create_time']=time_format($vo['create_time']);
           $vo['update_time']=time_format($vo['update_time']);
           $vo['manage_update_time']=time_format($vo['manage_update_time']);
           $vo['pay_evidence']=empty($vo['pay_evidence'])?"未缴费":'已缴费';
           if($vo['status']==0){
               $vo['status']='未审核';
           }else if($vo['status']==1){
               $vo['status']='已审核';
           }
       }
       exportexcel($list,array('编号','姓名','身份证号','家庭住址','毕业院校','专业','介绍人','介绍人电话','是否缴费','首次提交时间','最后一次修改时间','管理员修改时间','审核状态'),'非自主报名信息');
   }


    /**
     * 导出自主报名信息
     */
    public function exportVolunteer(){
        $map['a.type']=1;
        $status=I('get.status');
        if($status=='' || $status==null){
            $map['a.status']=array('gt',-1);
        }else{
            $map['a.status']=$status;
        }
        $id=I('get.id');
        if($id!='' && $id!=null){
            $map['a.id']=$id;
        }
        $major=I('get.major');
        if($major!='' && $major!=null){
            $where['title']=array('like','%'.$major.'%');
            $major=M('major')->where($where)->select();
            $ids = array_column($major, 'id');
            $map['a.major']=array('in',$ids);
        }
        $name=I('get.name');
        if($name!='' && $name!=null){
            $map['a.name']=array('like','%'.$name.'%');
        }

        $ID_number=I('get.ID_number');
        if($ID_number!='' && $ID_number!=null){
            $map['a.ID_number']=array('like','%'.$ID_number.'%');
        }
        $list=M('student_info')
            ->alias('a')
            ->join('vo_major as b ON a.major=b.id')
            ->field('a.id,a.name,a.ID_number,a.mobile,b.title,a.create_time,a.manage_update_time,a.status')
            ->where($map)
            ->select();
        foreach ($list as $k=>&$vo){
            $vo['create_time']=time_format($vo['create_time']);
            $vo['manage_update_time']=time_format($vo['manage_update_time']);
            if($vo['status']==0){
                $vo['status']='未审核';
            }else if($vo['status']==1){
                $vo['status']='已审核';
            }
        }
        exportPhpExcel($list,array('编号','姓名','身份证号','手机号码','感兴趣专业','首次提交时间','管理员修改时间','审核状态'),'自主报名信息');
    }



    /**
     *
     */
    public function exportType(){
        $type=I('get.type');
        switch ($type){
            case 'agent':
                $titlte="代理";
                $list=M('agent')->field('uid,username,nickname')->select();
                break;
            case 'department':
                $titlte="部门";
                $list=M('department_info')->field('id,title')->select();
                break;
            case 'major':
                $titlte="专业";
                $list=M('major')->field('id,title')->select();
                break;
        }
        $this->assign('title',$titlte);
        $this->assign('list',$list);
        $this->assign('type',$type);
        $this->display();
    }






    /**
     * 设置一条或者多条数据的状态
     * @author huajie <banhuajie@163.com>
     */
    public function setStatus($model='student_info'){
        return parent::setStatus('student_info');
    }



    /**
     * 更新一条数据
     * @author huajie <banhuajie@163.com>
     */
    public function update(){
        $res = D('Document')->update();
        if(!$res){
            $this->error(D('Document')->getError());
        }else{
            $this->success($res['id']?'更新成功':'新增成功', Cookie('__forward__'));
        }
    }



    /**
     * 还原被删除的数据
     * @author huajie <banhuajie@163.com>
     */
    public function permit(){
        /*参数过滤*/
        $ids = I('param.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }

        /*拼接参数并修改状态*/
        $Model  =   'Document';
        $map    =   array();
        if(is_array($ids)){
            $map['id'] = array('in', $ids);
        }elseif (is_numeric($ids)){
            $map['id'] = $ids;
        }
        $this->restore($Model,$map);
    }

    /**
     * 清空回收站
     * @author huajie <banhuajie@163.com>
     */
    public function clear(){
        $res = D('sudent_info')->remove();
        if($res !== false){
            $this->success('清空回收站成功！');
        }else{
            $this->error('清空回收站失败！');
        }
    }

    /**
     * 回收站列表
     * @author huajie <banhuajie@163.com>
     */
    public function recycle(){
        $map['status']  =   -1;
        $list = $this->lists(M('sudent_info'),$map,'update_time desc');
        $this->assign('list', $list);
        $this->meta_title  =   '回收站';
        $this->display();
    }


}
